/**
 * Created by zhourh on 2018/5/24.
 *
 * 给定一个正整数，检查他是否为交替位二进制数：换句话说，就是他的二进制数相邻的两个位数永不相等。
 *
 * 位运算&递归
 */
public class BinaryNumberWithAlternatingBits {

    public static void main(String[] args) {
        BinaryNumberWithAlternatingBits binaryNumberWithAlternatingBits = new BinaryNumberWithAlternatingBits();
        System.out.println(binaryNumberWithAlternatingBits.hasAlternatingBits(5));
        System.out.println(binaryNumberWithAlternatingBits.hasAlternatingBits(7));
        System.out.println(binaryNumberWithAlternatingBits.hasAlternatingBits(11));
        System.out.println(binaryNumberWithAlternatingBits.hasAlternatingBits(10));
    }

    public boolean hasAlternatingBits(int n) {
        boolean zero = (n & 0x01) == 0;

        if ((n >>= 1) != 0) {
            return (zero == ((n & 0x01) == 0)) ? false : hasAlternatingBits(n);
        }

        return true;
    }
}

